/*
 * Class Name: FCUBSPWDEncryption
 *
 * Date:       April 04 11:12 AM
 *
 * This source is part of the General Framework and is copyrighted by Oracle Financial Services Software Limited.
 *
 * All rights reserved.  No part of this work may be reproduced, stored in a
 * retrieval system, adopted or transmitted in any form or by any means,
 * electronic, mechanical, photographic, graphic, optic recording or otherwise,
 * translated in any language or computer language, without the prior written
 * permission of Oracle Financial Services Software Limited.
 *                       
 * Oracle Financial Services Software Limited.
 * 10-11, SDF I, SEEPZ, Andheri (East),
 * Mumbai - 400 096.
 * India
 *
 * Copyright � 2008- 2009 by Oracle Financial Services Software Limited.
 */
package common;

import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.soap.encoding.soapenc.Base64;

/*
 * This class is used to encrypt password according to FCUBS encryption
 * logic.
*/
public class Encryption 
{

    public Encryption()
	{}
	/**
     * <PRE>
     * Encrypt the password.
     * </PRE>
     *
     * @param  hashStr          A String variable representing password.
     *
	 * @param  userId           A String variable representing user id.
	 *
     * @exception  Exception    Thrown when some exception is thrown
     *
     * @return     String
     */
     public  String tripleDESpwd(String hashStr, String userId) throws Exception {
            userId				= Base64.encode(userId.getBytes());
            MessageDigest m1	= MessageDigest.getInstance("MD5");

            m1.update(userId.getBytes());

            byte [] userSeed	= new byte[24];
            byte [] digestSeed	= m1.digest();
            int j				= 0 ;
            for (int i=0; i<digestSeed.length; i++){
                userSeed[i]		= digestSeed[i];
            }
            for (int i =16;i<24;i++){
                userSeed[i]		= userSeed[j];
                j++;
            }
            m1.reset();
            m1.update(hashStr.getBytes());

            byte []pwdSeed		= m1.digest();
             byte [] encPwd		= doTripleDES(pwdSeed,userSeed);
             MessageDigest m	= MessageDigest.getInstance("MD5");
             m.update(encPwd);
             return toHexString(m.digest());
    }

	/**
     * <PRE>
     * Encrypt the password (Helper method of 'tripleDESpwd').
     * </PRE>
     */
      
    public static byte[] doTripleDES(byte[] toBeEncrypted, byte[] userSeed) throws Exception {
            toBeEncrypted			= Base64.encode(toBeEncrypted).getBytes("UTF-8");
            byte[] ret_array		= null;
            Cipher l_cipher;
            SecretKeySpec skspec	= null;
            try {
                l_cipher	= Cipher.getInstance("DESede/ECB/NoPadding");
                skspec		= new SecretKeySpec(userSeed, "DESede");
                l_cipher.init(Cipher.ENCRYPT_MODE, skspec);
                ret_array	= l_cipher.doFinal(toBeEncrypted);
                l_cipher	= null;
                skspec		= null;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                l_cipher	= null;
                skspec		= null;
            }
            return ret_array;
    }

	/**
     * <PRE>
     * Formation of Hexadecimal Characters for given Byte array.
     * </PRE>
     */

	 public static final String toHexString(byte[] bytes) {
        String HexChars[] = 
        { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", 
          "E", "F" };
        StringBuffer sb = new StringBuffer();
        int i;
        for (i = 0; i < bytes.length; i++) {
            sb.append(HexChars[(bytes[i] >> 4) & 0xf]);
            sb.append(HexChars[bytes[i] & 0xf]);
        }
        return new String(sb);
    }
}